package configurationReader;

import com.configurator.FieldData;
import com.google.common.collect.Maps;
import com.google.common.collect.Multiset.Entry;



import java.io.File;
import java.util.HashMap;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 * @author Sawior
 */
public class SpacesWritter {

    public static void writeSpaces(HashMap<Integer, FieldData> spaces) {
        try {
            
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

            // root elements
            Document doc = docBuilder.newDocument();
            Element rootElement = doc.createElement("spaces");
            doc.appendChild(rootElement);

            for (Integer key : spaces.keySet() ) {
                Element space = doc.createElement("space");
                rootElement.appendChild(space);

                Element number = doc.createElement("number");
                number.appendChild(doc.createTextNode(spaces.get(key).getSpaceNumber().toString()));
                space.appendChild(number);

                Element type = doc.createElement("type");
                type.appendChild(doc.createTextNode(spaces.get(key).getSpaceTypeToXML()));
                space.appendChild(type);

                Element status = doc.createElement("status");
                status.appendChild(doc.createTextNode(spaces.get(key).getSpaceStatusToXML()));
                space.appendChild(status);
                
                Element column = doc.createElement("column");
                column.appendChild(doc.createTextNode(spaces.get(key).getX().toString()));
                space.appendChild(column);
                
                Element row = doc.createElement("row");
                row.appendChild(doc.createTextNode(spaces.get(key).getY().toString()));
                space.appendChild(row);
                
                Element level = doc.createElement("level");
                level.appendChild(doc.createTextNode(spaces.get(key).getLevel().toString()));
                space.appendChild(level);
                
            }

            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File(".\\src\\main\\resources\\configSpaces.xml"));
            transformer.transform(source, result);
            Logger.getLogger(SpacesWritter.class.getName()).log(Level.SEVERE, "[CarPark] Spaces configurator has been sucessfully saved.");
        } catch (ParserConfigurationException pce) {
            Logger.getLogger(SpacesWritter.class.getName()).log(Level.SEVERE, "[CarPark] Spaces configurator error. Parse exception.", pce);
        } catch (TransformerException tfe) {
            Logger.getLogger(SpacesWritter.class.getName()).log(Level.SEVERE, "[CarPark] Spaces configurator error. Transformer exception", tfe);
        }
    }
}
